import { Injectable } from '@nestjs/common';
import { Repository } from 'typeorm';
import { ExamEntity } from './exam.entity';
import { InjectRepository } from '@nestjs/typeorm';

@Injectable()
export class ExamService {
  constructor(
    @InjectRepository(ExamEntity)
    private readonly examRepository: Repository<ExamEntity>,
  ) {}

  async getAll(): Promise<Partial<ExamEntity[]>> {
    return await this.examRepository
      .createQueryBuilder('exam_entity')
      .leftJoinAndSelect('exam_entity.questions', 'question')
      .getMany();
  }

  async getOne(id): Promise<ExamEntity | undefined> {
    return await this.examRepository
      .createQueryBuilder('exam_entity')
      .where('id=:id', { id })
      .getOne();
  }

  /* 删除 请求方式 : delete */
  async deleteById(id: string): Promise<any> {
    return await this.examRepository.delete(id);
  }

  async create(examBody: Partial<ExamEntity>) {
    const res = this.examRepository.create(examBody);
    return await this.examRepository.save(res);
  }
}
